package org.itboys.mobile.service.mongo.mobile.product;

import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.mobile.entity.mongo.cookbook.KitchenCookBook;
import org.itboys.mobile.entity.mongo.product.KitchenProductCookBook;
import org.itboys.mobile.service.mongo.mobile.cookbook.KitchenMobileCookBookService;
import org.itboys.mobile.service.mongo.web.cookbook.KitchenCookBookService;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2017年5月9日
 * 描述：花果山（农厨）_菜谱_食材_service
 */

/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：农厨_商品_关联_菜谱_service_mobile。
 *
 */
@Service
public class KitchenMobileProductCookBookService extends BaseMongoService<KitchenProductCookBook> {
    @Autowired
    private KitchenMobileCookBookService cookBookService;
    @Autowired
    private ResourceHolder resourceHolder ;



    @Resource(name = "kitchenDs")
    private MongoDataSource kitchenDataSource;

    @Override
    protected MongoDataSource getMongoDataSource() {
        return kitchenDataSource;
    }
    @Override
    protected Class<KitchenProductCookBook> getEntityClass() {
        return KitchenProductCookBook.class;
    }

    /**
     * 根据产品获取菜谱
     * @param productId
     */
    public List<Map<String,Object>> getCookBooksByProduct(Long productId ){
        List<Map<String,Object>> results=new ArrayList<Map<String,Object>>();
        Map<String,Object> param= new HashMap<String,Object>();
        param.put("isDeleted", 0);
        param.put("productId",productId);
       List<Long> cookBookIds=new ArrayList<Long>();
       List<KitchenProductCookBook> productCookBooks=super.list(param);
       for(KitchenProductCookBook productCookBook:productCookBooks){
           cookBookIds.add(productCookBook.getCookBookId());

       }
       if(ListUtil.isNull(cookBookIds))return  results;
       param.clear();
       param.put("id in",cookBookIds);
       List<KitchenCookBook> cookBooks = cookBookService.list(param);
       String imageRoot = resourceHolder.getStringValue("imgRoot");
       for(KitchenCookBook cookBook:cookBooks){
           Map<String,Object> result=new HashMap<String,Object>();
           result.put("id",cookBook.getId());
           result.put("name",cookBook.getName());
           result.put("viceName",cookBook.getViceName());
           result.put("image",imageRoot+cookBook.getImage());
           results.add(result);
       }
       return results;
    }

}
